<?php

/**
 * Flickr Photosets - Tagged Photos Page
 * Displays a visualization of all photos tagged with people.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 8.5.13
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets and Flickr
*/
$fp = new FlickrPhotosetsCanvas();

// If the user has disabled tagging
if (!$fp->config['enable_tagging']) {
	$fp->error(FlickrPhotosetsErrors::$tagging_disabled);
}

// Since no one is actually seeing the photos, we'll get all of them
$fp->flickr->setToken($fp->config['flickr_token']);

// We will get only their photos with people tagged
$photos = $fp->flickr->photos_search(array('user_id' => $fp->nsid, 'machine_tags' => 'fb:user=', 'extras' => 'machine_tags'));

// Go through each photo's machine tags and count up the number of times each person is tagged
$tags = array();
foreach ($photos['photo'] as $photo) {
	$machine_tags = explode(' ', $photo['machine_tags']);
	foreach ($machine_tags as $tag) {
		if (strpos($tag, 'fb:user') !== false) {
			$uid = substr($tag, 8);
			if (is_numeric($uid)) {
				$tags[$uid] += 1;
			}
		}
	}
}

// Class names we will use depending on how many times a person is tagged
$sizes = array(
	1 => 'not_popular',
	2 => 'not_very_popular',
	3 => 'somewhat_popular',
	4 => 'popular',
	5 => 'very_popular',
	6 => 'ultra_popular',
);

// Get the min and max values
$min_size = 1;
$max_size = 6;
$min_qty = min(array_values($tags));
$max_qty = max(array_values($tags));

// Find the range and step
$range = $max_qty - $min_qty;
$range = ($range == 0) ? 1 : $range;
$step = ($max_size - $min_size) / $range;

// Make an array with the information we need
$tagged_users = array();
foreach ($tags as $user => $number) {
	$size = round($min_size + (($number - $min_qty) * $step));
	$tagged_users[] = array('uid' => $user, 'count' => $number, 'size' => $sizes[$size]);
}

?>

<fb:title><?=$fp->get_name_fbml(true)?> Tagged Photos</fb:title>
<?=$fp->get_stylesheet('tagged')?>

<?=$fp->print_media_header()?>

<?=$fp->echo_tabs('tagged')?>

<?php if (count($tagged_users) < 1) {
	$fp->error(FlickrPhotosetsErrors::$no_tagged_photos_generic);
} else { ?>
	
	<p class="info_message">
		<?=count($tagged_users)?> people have been tagged in a total of <?=$photos['total']?> of <fb:name uid="<?=$fp->uid?>" possessive="true" /> Flickr photos.
	</p>
	
	<ul class="tag_cloud">
	<?php foreach ($tagged_users as $user) {
		echo '<li class="'.$user['size'].'"><span>'.$user['count'].' photos tagged of </span><a href="'.$fp->canvas_url.'/photos/of/'.$user['uid'].'/" title="'.$user['count'].' ';
		echo ($user['count'] == 1) ? 'photo' : 'photos';
		echo '" class="tag"><fb:name uid="'.$user['uid'].'" linked="false" capitalize="true" /></a></li>';
	} ?>
	</ul>
	
	<ul class="profile_pics">
		<?php foreach ($tagged_users as $user) {
			echo '<li><a href="'.$fp->canvas_url.'/photos/of/'.$user['uid'].'/"><fb:profile-pic uid='.$user['uid'].' size="square" linked="false" /></a></li>';
		} ?>
	</ul>
	
	<script>
	
	function hideLinks(element) {
		element.getLastChild().setStyle('display', 'none');
	}
	
	function showLinks(element) {
		element.getLastChild().setStyle('display', '');
	}
	
	</script>
	
<?php } ?>

<?=$fp->echo_footer(true)?>